package com.hbb20.utils;

import ohos.app.Context;
import ohos.data.DatabaseHelper;

import java.lang.ref.WeakReference;


public class Preferences {

    private static final String PREFERENCES_NAME = "preferences";


    private static WeakReference<Preferences> mPreferences;
    private final Context mContext;

    private Preferences(Context context) {
        mContext = context;
    }

    /**
     * get
     *
     * @param context
     * @return Preferences
     */
    public static Preferences get(Context context) {
        if (mPreferences == null || mPreferences.get() == null) {
            mPreferences = new WeakReference<>(new Preferences(context.getApplicationContext()));
        }
        return mPreferences.get();
    }

    private ohos.data.preferences.Preferences getSharedPreferences() {
        DatabaseHelper databaseHelper = new DatabaseHelper(mPreferences.get().mContext);// context入参类型为ohos.app.Context
        return databaseHelper.getPreferences(PREFERENCES_NAME);
    }

    public void clearPreferences() {

        getSharedPreferences().clear().flush();

    }

    /**
     * getString
     *
     * @param var1 var1
     * @param var2 var2
     * @return String
     */
    public String getString(String var1, String var2) {
        return getSharedPreferences().getString(var1, var2);
    }

    /**
     * putString
     *
     * @param var1 var1
     * @param var2 var1
     */
    public void putString(String var1, String var2) {
        getSharedPreferences().putString(var1, var2).flush();
    }

}
